package com.lijin.dao.provider;

import com.lijin.dao.LotteryInformationDao;
import com.lijin.domain.entity.LotteryInformation;
import com.lijin.domain.query.LotteryInformationQuery;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

/**
 * Created by gold-jx on 2017/9/12.
 */
public class LotteryInformationProvider {

  /**
   * 根据id修改
   */
  public String updateById(@Param("lotteryInformation") LotteryInformation lotteryInformation) {
    SQL sql = new SQL();
    sql.UPDATE(LotteryInformationDao.TABLE_NAME);
    if (!StringUtils.isEmpty(lotteryInformation.getLotteryCode())) {
      sql.SET("title=#{lottery_code}");
    }
    if (!StringUtils.isEmpty(lotteryInformation.getLotteryExpect())) {
      sql.SET("lottery_expect=#{lotteryExpect}");
    }
    if (!StringUtils.isEmpty(lotteryInformation.getOpenCode())) {
      sql.SET("open_code=#{openCode}");
    }
    if (!StringUtils.isEmpty(lotteryInformation.getOpenTime())) {
      sql.SET("open_time=#{openTime}");
    }
    if (!StringUtils.isEmpty(lotteryInformation.getState())) {
      sql.SET("state=#{state}");
    }
    if (!StringUtils.isEmpty(lotteryInformation.getVersion())) {
      sql.SET("version=#{version}");
    }
    if (!StringUtils.isEmpty(lotteryInformation.getId())) {
      sql.WHERE("id= #{id}");
    }
    return sql.toString();
  }

  public String findCountByQuery(@Param("query") LotteryInformationQuery query) {

    SQL sql = new SQL();
    sql.SELECT("count(id)");
    sql.FROM(LotteryInformationDao.TABLE_NAME);

    if (!StringUtils.isEmpty(query.getLotteryCode())) {
      sql.WHERE("lottery_code = #{lotteryCode}");
    }
    if (!StringUtils.isEmpty(query.getLotteryExpect())) {
      sql.WHERE("lottery_expect = #{lotteryExpect}");
    }
    if (!StringUtils.isEmpty(query.getOpenCode())) {
      sql.WHERE("open_code = #{openCode}");
    }
    if (!StringUtils.isEmpty(query.getOpenTime())) {
      sql.WHERE("open_time = #{openTime}");
    }
    sql.WHERE("status != 'DELETED'");
    return sql.toString();
  }

  public String findListByQuery(@Param("query") LotteryInformationQuery query) {

    SQL sql = new SQL();
    sql.SELECT(LotteryInformationDao.COLUMNS);
    sql.FROM(LotteryInformationDao.TABLE_NAME);

    if (!StringUtils.isEmpty(query.getLotteryCode())) {
      sql.WHERE("lottery_code = #{lotteryCode}");
    }
    if (!StringUtils.isEmpty(query.getLotteryExpect())) {
      sql.WHERE("lottery_expect = #{lotteryExpect}");
    }
    if (!StringUtils.isEmpty(query.getOpenCode())) {
      sql.WHERE("open_code = #{openCode}");
    }
    if (!StringUtils.isEmpty(query.getOpenTime())) {
      sql.WHERE("open_time = #{openTime}");
    }
    sql.WHERE("status != 'DELETED'");
    sql.ORDER_BY("id desc");
    if (query.getPageSize() != null && query.getPageSize() != 0) {
      return sql.toString() + " limit #{start},#{pageSize}";
    }
    return sql.toString();
  }


  public String insertAll(Map map) {
    List<LotteryInformation> users = (List<LotteryInformation>) map.get("list");
    StringBuilder sb = new StringBuilder();
    sb.append("INSERT INTO lottery_information ");
    sb.append(
        "(lottery_code, lottery_expect, open_code, open_time, create_time, state, version, source, orig_time) ");
    sb.append("VALUES ");
    MessageFormat mf = new MessageFormat(
        "(#'{'list[{0}].lotteryCode},#'{'list[{0}].lotteryExpect},#'{'list[{0}].openCode},#'{'list[{0}].openTime},now(),#'{'list[{0}].state},#'{'list[{0}].version},#'{'list[{0}].source},now())");
    for (int i = 0; i < users.size(); i++) {
      sb.append(mf.format(new Object[]{i}));
      if (i < users.size() - 1) {
        sb.append(",");
      }
    }
    return sb.toString();
  }
}
